﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiffenGUI.BiffenService;
using System.ServiceModel;

namespace BiffenGUI.UserControls
{
    public partial class MemberAddBookingCreate : UserControl
    {
        private MemberAddBookingCurrent parent = null;
        public MemberAddBookingCurrent CurrentMoviesUserControl { get { return parent; } }
        private BiffenServiceClient service = null;
        private List<Show> shows = null;
        private Show selectedShow = null;
        private ShowDetails selectedShowControl = null;
        private Movie selectedMovie = null;
        private int memberId = 0;

        public MemberAddBookingCreate()
        {
            InitializeComponent();
        }

        public void SetReferances(MemberAddBookingCurrent p, BiffenServiceClient svc)
        {
            parent = p;
            service = svc;
            ucSeats.ServiceClient = service;
        }

        public void LoadShows()
        {
            // Setting price label to "0 kr."
            lblPriceValue.Text = "0 kr.";

            // Remove ShowDetail controls
            panelShows.Controls.Clear();

            // start x,y : 5, 190
            shows = service.GetShowsForMovie(selectedMovie.Id);
            int x = 5; int y = 5;
            int height = 65 + 5;

            foreach (Show s in shows)
            {
                ShowDetails sd = new ShowDetails();
                sd.Update(s, this);
                sd.Location = new Point(x, y);
                panelShows.Controls.Add(sd);
                y += height;
            }

            if (parent.MemberForm.CurrentLogin != null)
            {
                memberId = parent.MemberForm.CurrentLogin.Member.Id;
                txtPhone.Text = parent.MemberForm.CurrentLogin.Member.Phone.ToString();
            }
        }

        public void UpdatePrice()
        {
            txtPhone.Focus();

            if (selectedMovie != null)
            {
                decimal price = selectedMovie.PriceCategory.Price;
                lblPriceValue.Text = (price * ucSeats.GetAllSeats().Count).ToString() + " kr.";
            }
            else
            {
                lblPriceValue.Text = "0,00 kr.";
            }
        }

        public void UpdatePhoneNumber(string phone)
        {
            txtPhone.Text = phone;
        }

        public void SetSelectedMovie(Movie m)
        {
            selectedMovie = m;
            if (m != null)
            {
                LoadShows();
                movieDetails.Update(m, null, true);
            }
        }

        public void SetSelectedShow(Show s, ShowDetails sd)
        {
            if (s != null)
                ucSeats.UpdateShow(s);
            else
                ucSeats.Clear();

            selectedShow = s;
            if (selectedShowControl != null)
                selectedShowControl.BackColor = Color.Transparent;
            selectedShowControl = sd;

            UpdatePrice();
        }

        private void lblHeader_Click(object sender, EventArgs e)
        {
            parent.PrevStep();
        }

        private void bAdd_Click(object sender, EventArgs e)
        {
            if (parent.MemberForm.CurrentLogin != null)
            {
                memberId = parent.MemberForm.CurrentLogin.Member.Id;
            }
            if (txtPhone.Text != "")
            {
                int phone;
                if (int.TryParse(txtPhone.Text, out phone))
                {
                    List<int> seats = ucSeats.GetAllSeats();
                    try
                    {
                        service.CreateBooking((int)Booking.States.Reserved, phone, 1, selectedShow.Id, seats, memberId);
                        txtPhone.Text = "";
                        parent.PrevStep();
                        MessageBox.Show("Booking tilføjet!", "Booking tilføjet", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (FaultException<SeatsAlreadyReservedFault>)
                    {
                        MessageBox.Show("Sæderne du forsøgte at reservere, er allerede reserveret.", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                }
            }
            else
            {

            }
        }

        private void txtPhone_KeyDown(object sender, KeyEventArgs e)
        {
            e.SuppressKeyPress = Tools.ValidateKeyPressAsNumber(e);
        }

        private void txtPrice_TextChanged(object sender, EventArgs e)
        {

        }

    }
}
